package main

import (
	"context"
	"fmt"

	"gitee.com/wu-jin-feng/kinfu/fgin"
	reflectPb "gitee.com/wu-jin-feng/kinfu/fgin/demos/fvalidator_test/http_grpc/pb"
	"github.com/gin-gonic/gin"
)

type getSchemeReqInput struct {
	Path string    `json:"path" validate:"required" label:"路径"`
	Info infoInput `json:"info"`
}

type infoInput struct {
	Info string `json:"info" validate:"required"`
	Name string `json:"name" validate:"required"`
}

type DemoServiceServer struct {
	reflectPb.UnimplementedDemoServiceServer
}

func (s *DemoServiceServer) GetScheme(ctx context.Context, req *reflectPb.GetSchemeReq) (*reflectPb.GetSchemeResp, error) {
	valiInput := &getSchemeReqInput{
		Path: req.Path,
		Info: infoInput{
			Info: req.Info.Info,
			Name: req.Info.Name,
		},
	}
	if err := fgin.ValidateStruct(valiInput); err != nil {
		return nil, err
	}
	endData := &reflectPb.GetSchemeResp{
		Data: req.Path + ",gogogo",
		Info: &reflectPb.GetSchemeRespInfo{
			Info: fmt.Sprintf("这是一个info,%s", req.Info.Info),
			Name: fmt.Sprintf("作者是%s", req.Info.Name),
		},
	}
	return endData, nil
}

func main() {
	// fgin.RegisterGrpcServerOptions([]grpc.ServerOption{}, true, true)
	f := fgin.Fgin{}
	r, s := f.GetHttpGrpcServer("./test.yaml")
	// 注册grpc服务进去
	reflectPb.RegisterDemoServiceServer(s, &DemoServiceServer{})
	// http服务
	r.POST("/test1", func(ctx *gin.Context) {
		input := &getSchemeReqInput{}
		if err := ctx.ShouldBind(input); err != nil {
			fgin.ReturnJson(ctx, 400, nil, err)
			return
		}
		demoObj := &DemoServiceServer{}
		end, err := demoObj.GetScheme(ctx, &reflectPb.GetSchemeReq{
			Path: input.Path,
			Info: &reflectPb.GetSchemeRespInfo{
				Info: input.Info.Info,
				Name: input.Info.Name,
			},
		})
		if err != nil {
			fgin.ReturnJson(ctx, 400, nil, err)
			return
		}
		fgin.ReturnJson(ctx, 200, end, nil)
	})
	f.RunHttpGrpc(r, s)
}
